package com.base.commonlib.utils;

import com.base.commonlib.base.BaseApp;
import com.base.commonlib.bean.CheckRecordEntity;
import com.base.commonlib.bean.CheckRecordEntityDao;
import com.base.commonlib.bean.DaoSession;
import com.base.commonlib.bean.FieldAttachmentEntity;
import com.base.commonlib.bean.FieldAttachmentEntityDao;
import com.base.commonlib.bean.FieldInspectionEntity;
import com.base.commonlib.bean.FieldInspectionEntityDao;
import com.base.commonlib.bean.FieldUserEntity;
import com.base.commonlib.bean.FieldUserEntityDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weikailiang on 2019/11/14.
 */

public class CheckDaoUtils {

    //插入一条检查数据
    public static boolean insertCheck(FieldInspectionEntity mEntity){
        //插入主体
        try {
            FieldInspectionEntityDao partnerDao = BaseApp.getDaoSession().getFieldInspectionEntityDao();
            QueryBuilder<FieldInspectionEntity> partnerQueryBuilder = partnerDao.queryBuilder();
            List<FieldInspectionEntity> list = partnerQueryBuilder.where(FieldInspectionEntityDao.Properties.LocalId.eq(mEntity.getLocalId())).list();
            if(list!=null)
            {
                partnerDao.deleteInTx(list);
            }
            partnerDao.insert(mEntity);
            //插入现场检查项实体
            List<CheckRecordEntity> mCheckRecordList = mEntity.getCheckRecordRequestList();
            CheckRecordEntityDao mCheckRecordDao = BaseApp.getDaoSession().getCheckRecordEntityDao();
            QueryBuilder<CheckRecordEntity> mCheckRecordBuild = mCheckRecordDao.queryBuilder();
            List<CheckRecordEntity> mCheckList = mCheckRecordBuild.where(CheckRecordEntityDao.Properties.LocalId.eq(mEntity.getLocalId())).list();
            if (mCheckList != null){
                mCheckRecordDao.deleteInTx(mCheckList);
            }
            for (CheckRecordEntity checkRecordEntity : mCheckRecordList){
                mCheckRecordDao.insert(checkRecordEntity);
            }

            //插入现场检查图片实体
            List<FieldAttachmentEntity> mFieldAttachmentList = mEntity.getAttachmentRequestList();
            FieldAttachmentEntityDao mFieldAttachmentDao = BaseApp.getDaoSession().getFieldAttachmentEntityDao();
            QueryBuilder<FieldAttachmentEntity> mFieldAttachmentBuild = mFieldAttachmentDao.queryBuilder();
            List<FieldAttachmentEntity> mFieldAttachment = mFieldAttachmentBuild.where(FieldAttachmentEntityDao.Properties.LocalId.eq(mEntity.getLocalId())).list();
            if (mFieldAttachment !=null){
                mFieldAttachmentDao.deleteInTx(mFieldAttachment);
            }
            for (FieldAttachmentEntity fieldAttachmentEntity : mFieldAttachmentList){
                mFieldAttachmentDao.insert(fieldAttachmentEntity);
            }

            //插入搭档
            List<FieldUserEntity> mFieldUserList = mEntity.getFieldUserRequestList();
            FieldUserEntityDao mFieldUserDao = BaseApp.getDaoSession().getFieldUserEntityDao();
            QueryBuilder<FieldUserEntity> mFieldUserBuild = mFieldUserDao.queryBuilder();
            List<FieldUserEntity> mFieldUser = mFieldUserBuild.where(FieldUserEntityDao.Properties.LocalId.eq(mEntity.getLocalId())).list();
            if (mFieldUser != null){
                mFieldUserDao.deleteInTx(mFieldUser);
            }
            for (FieldUserEntity fieldUserEntity : mFieldUserList){
                mFieldUserDao.insert(fieldUserEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
       return true;
    }
    //查询检查列表
    public static List<FieldInspectionEntity> getFieldInspectionEntityList(String userId){
        DaoSession daoSession = BaseApp.getDaoSession();
        QueryBuilder<FieldInspectionEntity> partnerQueryBuilder = daoSession.getFieldInspectionEntityDao().queryBuilder();
        return partnerQueryBuilder.where(FieldInspectionEntityDao.Properties.Login_user_id.eq(userId)).list();
    }
    //查询一跳检查数据
    public static FieldInspectionEntity getFieldInspectionEntity(String localId){
        DaoSession daoSession = BaseApp.getDaoSession();
        QueryBuilder<FieldInspectionEntity> partnerQueryBuilder = daoSession.getFieldInspectionEntityDao().queryBuilder();
        FieldInspectionEntity bean = partnerQueryBuilder.where(FieldInspectionEntityDao.Properties.LocalId.eq(localId)).unique();
        //查询图片
        QueryBuilder<FieldAttachmentEntity> fieldAttachmentEntityQueryBuilder = daoSession.getFieldAttachmentEntityDao().queryBuilder();
        List<FieldAttachmentEntity> fieldAttachmentEntities = fieldAttachmentEntityQueryBuilder.where(FieldAttachmentEntityDao.Properties.LocalId.eq(localId)).list();
        bean.setAttachmentRequestList(fieldAttachmentEntities);
        //查询现场检查项实体
        QueryBuilder<CheckRecordEntity> checkRecordEntityQueryBuilder = daoSession.getCheckRecordEntityDao().queryBuilder();
        List<CheckRecordEntity> checkRecordEntityList = checkRecordEntityQueryBuilder.where(CheckRecordEntityDao.Properties.LocalId.eq(localId)).list();
        bean.setCheckRecordRequestList(checkRecordEntityList);
        //查询搭档
        QueryBuilder<FieldUserEntity> fieldUserEntityQueryBuilder = daoSession.getFieldUserEntityDao().queryBuilder();
        List<FieldUserEntity> fieldUserEntities = fieldUserEntityQueryBuilder.where(FieldUserEntityDao.Properties.LocalId.eq(localId)).list();
        bean.setFieldUserRequestList(fieldUserEntities);
        return bean;
    }

    public static List<FieldInspectionEntity> getFieldInspectionEntityList(){
        List<FieldInspectionEntity> mData = new ArrayList<>();
       List<FieldInspectionEntity> list = getFieldInspectionEntityList(BaseApp.getUser().getUserId());
       for (FieldInspectionEntity fieldInspectionEntity : list){
           mData.add(getFieldInspectionEntity(fieldInspectionEntity.getLocalId()));
       }
       return mData;
    }

    //删除一条本地检查数据
    public static void deleteFieldInspectionEntity(String localId){
        DaoSession daoSession = BaseApp.getDaoSession();
        QueryBuilder<FieldInspectionEntity> partnerQueryBuilder = daoSession.getFieldInspectionEntityDao().queryBuilder();
        FieldInspectionEntity bean = partnerQueryBuilder.where(FieldInspectionEntityDao.Properties.LocalId.eq(localId)).unique();
        daoSession.getFieldInspectionEntityDao().delete(bean);
        //查询图片
        QueryBuilder<FieldAttachmentEntity> fieldAttachmentEntityQueryBuilder = daoSession.getFieldAttachmentEntityDao().queryBuilder();
        List<FieldAttachmentEntity> fieldAttachmentEntities = fieldAttachmentEntityQueryBuilder.where(FieldAttachmentEntityDao.Properties.LocalId.eq(localId)).list();
        daoSession.getFieldAttachmentEntityDao().deleteInTx(fieldAttachmentEntities);
        //查询现场检查项实体
        QueryBuilder<CheckRecordEntity> checkRecordEntityQueryBuilder = daoSession.getCheckRecordEntityDao().queryBuilder();
        List<CheckRecordEntity> checkRecordEntityList = checkRecordEntityQueryBuilder.where(CheckRecordEntityDao.Properties.LocalId.eq(localId)).list();
        daoSession.getCheckRecordEntityDao().deleteInTx(checkRecordEntityList);
        //查询搭档
        QueryBuilder<FieldUserEntity> fieldUserEntityQueryBuilder = daoSession.getFieldUserEntityDao().queryBuilder();
        List<FieldUserEntity> fieldUserEntities = fieldUserEntityQueryBuilder.where(FieldUserEntityDao.Properties.LocalId.eq(localId)).list();
        daoSession.getFieldUserEntityDao().deleteInTx(fieldUserEntities);
    }

    //更新上报状态
    public static void updataFieldInspectionEntity(String localId,String id){
        DaoSession daoSession = BaseApp.getDaoSession();
        QueryBuilder<FieldInspectionEntity> partnerQueryBuilder = daoSession.getFieldInspectionEntityDao().queryBuilder();
        FieldInspectionEntity bean = partnerQueryBuilder.where(FieldInspectionEntityDao.Properties.LocalId.eq(localId)).unique();
        bean.setIsUp(true);
        bean.setId(id);
        daoSession.getFieldInspectionEntityDao().update(bean);
    }
}
